<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.app
 */
class AppController extends Controller {
	
	function getCategories(){
		//get categories
		$this->loadModel('Category');
		$this->Category->recursive = -1;
		$this->set('categories', $this->Category->find('list', array('conditions' => array('id != ' => 4))));
	}
	
	function getTags(){
		$this->loadModel('Article');
		$this->Article->recursive = -1;
		$results = $this->Article->find('list', array('fields' => array('Article.id', 'Article.tags'), 'order' => array('rand()'), 'limit' => 20));
		$tags	 = '';
		foreach($results as $key => $val){
			if(!empty($val)){
				$vas = explode(',', $val);
				shuffle($vas);
				$rands = array_rand($vas, 1);
				$tags[$key] = $vas[$rands];
			}
		}
		$this->set(compact('tags'));
	}
	
	function _check(){
		$user_id = $this->Session->read('User.id');
		if(empty($user_id)){
			$this->Session->destroy();
			$this->redirect(array('admin' => true, 'controller' => 'users', 'action' => 'login'));
		}
		$this->set('user_id', $user_id);
	}
}